/**
 * 初始化杂项管理详情对话框
 */
var TOtherFeeInfoDlg = {
    tOtherFeeInfoData : {},
    validateFields: {
        feeType: {
            validators: {
                notEmpty: {
                    message: '费用名称不能为空'
                }
            }
        },
        money: {
            validators: {
                notEmpty: {
                    message: '金额不能为空'
                }
            }
        },
        applicant: {
            validators: {
                notEmpty: {
                    message: '申请人不能为空'
                }
            }
        }
    }
};
TOtherFeeInfoDlg.validate = function () {
    $('#orderFeeFrom').data("bootstrapValidator").resetForm();
    $('#orderFeeFrom').bootstrapValidator('validate');
    return $("#orderFeeFrom").data('bootstrapValidator').isValid();
};

/**
 * 清除数据
 */
TOtherFeeInfoDlg.clearData = function() {
    this.tOtherFeeInfoData = {};
}

/**
 * 设置对话框中的数据
 *
 * @param key 数据的名称
 * @param val 数据的具体值
 */
TOtherFeeInfoDlg.set = function(key, val) {
    this.tOtherFeeInfoData[key] = (typeof val == "undefined") ? $("#" + key).val() : val;
    return this;
}

/**
 * 设置对话框中的数据
 *
 * @param key 数据的名称
 * @param val 数据的具体值
 */
TOtherFeeInfoDlg.get = function(key) {
    return $("#" + key).val();
}

/**
 * 关闭此对话框
 */
TOtherFeeInfoDlg.close = function() {
    parent.layer.close(window.parent.TOtherFee.layerIndex);
}

/**
 * 收集数据
 */
TOtherFeeInfoDlg.collectData = function() {
    this
    .set('id')
    .set('orderId',$('#order_id').val())
    .set('feeType')
    .set('money')
    .set('remark')
    .set('applicant')
    ;
}

/**
 * 提交添加
 */
TOtherFeeInfoDlg.addSubmit = function() {

    this.clearData();
    this.collectData();

    // if($("#order_id").val()==''){
    //     Feng.error("请关联单号!");
    //     return;
    // }


    if (!this.validate()) {
        return;
    }

    //提交信息
    var ajax = new $ax(Feng.ctxPath + "/tOtherFee/add", function(data){
        Feng.success("添加成功!");
        window.parent.aTable.refresh();
        TOtherFeeInfoDlg.close();
    },function(data){
        Feng.error("添加失败!" + data.responseJSON.message + "!");
    });
    ajax.set(this.tOtherFeeInfoData);
    ajax.start();
}

/**
 * 提交修改
 */
TOtherFeeInfoDlg.editSubmit = function() {

    this.clearData();
    this.collectData();

    //提交信息
    var ajax = new $ax(Feng.ctxPath + "/tOtherFee/update", function(data){
        Feng.success("修改成功!");
        window.parent.TOtherFee.table.refresh();
        TOtherFeeInfoDlg.close();
    },function(data){
        Feng.error("修改失败!" + data.responseJSON.message + "!");
    });
    ajax.set(this.tOtherFeeInfoData);
    ajax.start();
}

$(function() {
    Feng.initValidator("orderFeeFrom", TOtherFeeInfoDlg.validateFields);

    $("#orderNum").bsSuggest('init', {
        /*url: "/rest/sys/getuserlist?keyword=",
        searchFields: [ "shortAccount"],
        effectiveFieldsAlias:{userName: "姓名"},*/
        effectiveFields: ["id", "original_num"],
        clearable: true,
        allowNoKeyword: false,
        url: Feng.ctxPath + "/tOrder/ordersNum?query=",
        getDataMethod: "url",
        idField: "id",
        delayUntilKeyup: true,
        keyField: "original_num",
        fnPreprocessKeyword: function (keyword) {
            return encodeURI(keyword, "UTF-8");
        },
        processData: function (result) {
            var data = { value: [] };
            // for(var i=0;result.length<i;i++)
            $.each(result, function (i, r) {
                data.value.push({ original_num: r.original_num,id:r.id })
            })
            return data;
        }
    }).on('onDataRequestSuccess', function (e, result) {
        // console.log('onDataRequestSuccess: ', result);
    }).on('onSetSelectValue', function (e, keyword, data) {
        console.log('onSetSelectValue: ', keyword, data);
        // alert(keyword.id)
        $("#order_id").val(keyword.id);
    }).on('onUnsetSelectValue', function () {
        // console.log('onUnsetSelectValue');
        $("#order_id").val('');
    });
});
